package _9_日期与时间;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 本地日期和时间： LocalDateTime，LocalDate，LocalTime；
 * 格式化类型：DateTimeFormatter
 *  since java 8
 *  https://www.liaoxuefeng.com/wiki/1252599548343744/1303871087444002
 */
public class _2_LocalDateTimeTest {

    /**
     * LocalDateTime、LocalDate、LocalTime
     * 它表示一个本地日期和时间
     */
    @Test
    public void printLocalDateLocalTimeLocalDateTimeTest(){
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDate: " + localDate);// 严格按照ISO 8601格式打印
        System.out.println("localTime: " + localTime);// 严格按照ISO 8601格式打印
        System.out.println("localDateTime: " + localDateTime);// 严格按照ISO 8601格式打印
    }

    @Test
    public void printLocalDateLocalTimeLocalDateTimeTest2(){
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = localDateTime.toLocalDate();
        LocalTime localTime = localDateTime.toLocalTime();
        System.out.println("localDate: " + localDate);// 严格按照ISO 8601格式打印
        System.out.println("localTime: " + localTime);// 严格按照ISO 8601格式打印
        System.out.println("localDateTime: " + localDateTime);// 严格按照ISO 8601格式打印
    }

    /**
     * 通过指定的日期和时间创建LocalDateTime可以通过of()方法：
     */
    @Test
    public void ofLocalDateLocalTimeLocalDateTime(){
        LocalDate localDate = LocalDate.of(2020,6,4);
        LocalTime localTime = LocalTime.of(10,13,40) ;
        LocalDateTime localDateTime1 = LocalDateTime.of(localDate,localTime);
        LocalDateTime localDateTime2 = LocalDateTime.of(2020,6,4,10,13,40);
        System.out.println("localDate: " + localDate);
        System.out.println("localTime: " + localTime);
        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
    }

    /**
     * 字符串转换为LocalDateTime (严格按照ISO 8601的格式)
     *
     * 日期：yyyy-MM-dd
     * 时间：HH:mm:ss
     * 带毫秒的时间：HH:mm:ss.SSS
     * 日期和时间：yyyy-MM-dd'T'HH:mm:ss
     * 带毫秒的日期和时间：yyyy-MM-dd'T'HH:mm:ss.SSS
     */
    @Test
    public void parseLocalDateLocalTimeLocalDateTime(){
        LocalDateTime localDateTime = LocalDateTime.parse("2020-06-04T14:49:06");
        LocalDate localDate = LocalDate.parse("2020-06-04");
        LocalTime localTime = LocalTime.parse("14:51:44");
        System.out.println("localDate: " + localDate);
        System.out.println("localTime: " + localTime);
        System.out.println("localDateTime: " + localDateTime);
    }

    /**
     * DateTimeFormatter (非ISO 8601格式的字符串解析成LocalDateTime)
     */
    //自定义格式化
    @Test
    public void dateTimeFormatterTest(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateTimeStr = dateTimeFormatter.format(localDateTime);
        System.out.println(dateTimeStr);
    }

    //用自定义格式解析
    @Test
    public void dateTimeFormatterTest2(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse("2020/06/04 15:02:50", dateTimeFormatter);
        System.out.println(localDateTime);
    }

    /**
     * LocalDateTime 时间、日期 的 加减
     */
    @Test
    public void plusMinusLocalDateTimeTest(){
        LocalDateTime localDateTime = LocalDateTime.of(2020,04,06,15,8,36);
        System.out.println(localDateTime);

        //加5天 减3小时
        localDateTime = localDateTime.plusDays(5).minusHours(3);
        System.out.println(localDateTime);

        //减1个月
        LocalDateTime localDateTime2 = localDateTime.minusMonths(1);
        System.out.println(localDateTime2);
    }

    /**
     * 对日期和时间进行调整则使用withXxx()方法
     *
     * 调整年：withYear()
     * 调整月：withMonth()
     * 调整日：withDayOfMonth()
     * 调整时：withHour()
     * 调整分：withMinute()
     * 调整秒：withSecond()
     */
    @Test
    public void withLocalDateTimeTest(){
        LocalDateTime localDateTime = LocalDateTime.of(2020,06,04,15,24,19);
        System.out.println(localDateTime);//2020-06-04T15:24:19
        localDateTime = localDateTime.withDayOfMonth(30);
        System.out.println(localDateTime);//2020-06-30T15:24:19
        localDateTime = localDateTime.withMonth(9);
        System.out.println(localDateTime);//2020-09-30T15:24:19
    }

    @Test
    public void withLocalDateTimeTest2(){
        //本月第一天0:00时刻:
        LocalDateTime localDateTime = LocalDate.now().withDayOfMonth(1).atStartOfDay();
        System.out.println(localDateTime);//2020-06-01T00:00

        //本月最后1天:
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(localDate);//2020-06-30

        //下月第1天:
        LocalDate localDate1 = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(localDate1);//2020-07-01

        //本月第1个周一:
        LocalDate localDate2 = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        System.out.println(localDate2);//2020-06-01
    }

    /**
     * 判断两个LocalDateTime的先后
     */
    @Test
    public void beforeAfterLocalDateTime(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime target = LocalDateTime.of(2019,11,19,8,15,0);
        System.out.println(now.isBefore(target));//false
        System.out.println(LocalDate.now().isBefore(LocalDate.of(2019,11,19)));//false
        System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));//true
    }

    /**
     *  计算两个日期 + - * 、
     *  还本付息
     */
    @Test
    public void f(){
//        LocalDate localDate1 = LocalDate.parse("2018-01-15");
//        LocalDate localDate2 = LocalDate.parse("2018-02-21");


        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate localDate1 = LocalDate.parse("2018/01/21", dateTimeFormatter);
        LocalDate localDate2 = LocalDate.parse("2018/02/15", dateTimeFormatter);


        long day1 = localDate1.toEpochDay();
        long day2 = localDate2.toEpochDay();
        BigDecimal dayNum_BigDecimal = new BigDecimal(day1 > day2 ? day1 - day2 : day2 - day1);
//        System.out.println("两个日期相差： " + (day2 - day1));

//        double d = 10000 * 0.12 / 360;
//        System.out.println(d);
        BigDecimal benJin_BigDecimal = new BigDecimal(10000);
        BigDecimal nianLiLv_BigDecimal = new BigDecimal(0.12);
        BigDecimal nianShu_BigDecimal = new BigDecimal(360);
        BigDecimal result_BigDecimal = benJin_BigDecimal.multiply(nianLiLv_BigDecimal)
                .divide(nianShu_BigDecimal,15,BigDecimal.ROUND_HALF_UP)
                .multiply(dayNum_BigDecimal).setScale(2,BigDecimal.ROUND_HALF_UP);

        System.out.println(result_BigDecimal.toString());
    }
}
